package cn.iocoder.yudao.module.energy.dal.mysql.report;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.energy.controller.admin.report.vo.EnergyReportDataPageReqVO;
import cn.iocoder.yudao.module.energy.dal.dataobject.report.EnergyReportDataDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 能耗报告数据 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface EnergyReportDataMapper extends BaseMapperX<EnergyReportDataDO> {

    default PageResult<EnergyReportDataDO> selectPage(EnergyReportDataPageReqVO reqVO) {
        LambdaQueryWrapperX<EnergyReportDataDO> query = new LambdaQueryWrapperX<EnergyReportDataDO>()
            .eqIfPresent(EnergyReportDataDO::getDeviceId, reqVO.getDeviceId())
            .eqIfPresent(EnergyReportDataDO::getReportDataType, reqVO.getReportDataType())
            .eqIfPresent(EnergyReportDataDO::getReportDataLevel, reqVO.getReportDataLevel())
            .eqIfPresent(EnergyReportDataDO::getStatus, reqVO.getStatus())
            .eqIfPresent(EnergyReportDataDO::getUploadType, reqVO.getUploadType())
            .betweenIfPresent(EnergyReportDataDO::getReportDataTime, reqVO.getReportDataTime())
            .betweenIfPresent(EnergyReportDataDO::getCreateTime, reqVO.getCreateTime())
            .geIfPresent(EnergyReportDataDO::getEnergyConsumption, reqVO.getMinEnergyConsumption())
            .leIfPresent(EnergyReportDataDO::getEnergyConsumption, reqVO.getMaxEnergyConsumption())
            .orderByDesc(EnergyReportDataDO::getId);

        // 如果设置了uploadTypeExists为true，则只查询uploadType有值的数据
        if (Boolean.TRUE.equals(reqVO.getUploadTypeExists())) {
            query.isNotNull(EnergyReportDataDO::getUploadType);
        }
            
        return selectPage(reqVO, query);
    }
    
    /**
     * 查询列表，用于导出
     *
     * @param reqVO 查询条件
     * @return 能耗报告数据列表
     */
    default List<EnergyReportDataDO> selectList(EnergyReportDataPageReqVO reqVO) {
        LambdaQueryWrapperX<EnergyReportDataDO> query = new LambdaQueryWrapperX<EnergyReportDataDO>()
            .eqIfPresent(EnergyReportDataDO::getDeviceId, reqVO.getDeviceId())
            .eqIfPresent(EnergyReportDataDO::getReportDataType, reqVO.getReportDataType())
            .eqIfPresent(EnergyReportDataDO::getReportDataLevel, reqVO.getReportDataLevel())
            .eqIfPresent(EnergyReportDataDO::getStatus, reqVO.getStatus())
            .eqIfPresent(EnergyReportDataDO::getUploadType, reqVO.getUploadType())
            .betweenIfPresent(EnergyReportDataDO::getReportDataTime, reqVO.getReportDataTime())
            .betweenIfPresent(EnergyReportDataDO::getCreateTime, reqVO.getCreateTime())
            .geIfPresent(EnergyReportDataDO::getEnergyConsumption, reqVO.getMinEnergyConsumption())
            .leIfPresent(EnergyReportDataDO::getEnergyConsumption, reqVO.getMaxEnergyConsumption())
            .orderByDesc(EnergyReportDataDO::getId);
            
        // 如果设置了uploadTypeExists为true，则只查询uploadType有值的数据
        if (Boolean.TRUE.equals(reqVO.getUploadTypeExists())) {
            query.isNotNull(EnergyReportDataDO::getUploadType);
        }
        
        return selectList(query);
    }
    
    /**
     * 直接更新指定字段
     *
     * @param id             ID
     * @param reportDataTime 上报数据时间
     * @return 更新结果
     */
    @Update("UPDATE energy_report_data SET report_data_time = #{reportDataTime} WHERE id = #{id}")
    int updateReportDataTime(@Param("id") Long id, @Param("reportDataTime") String reportDataTime);
    
    /**
     * 更新累计能耗
     *
     * @param id                ID
     * @param energyConsumption 累计能耗
     * @return 更新结果
     */
    @Update("UPDATE energy_report_data SET energy_consumption = #{energyConsumption} WHERE id = #{id}")
    int updateEnergyConsumption(@Param("id") Long id, @Param("energyConsumption") String energyConsumption);

    /**
     * 根据设备ID和时间范围查询报告数据
     *
     * @param deviceId  设备ID，为空则查询所有设备
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 报告数据列表
     */
    default List<EnergyReportDataDO> selectByDeviceAndTimeRange(String deviceId, LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapperX<EnergyReportDataDO> queryWrapper = new LambdaQueryWrapperX<>();
        
        // 设备ID条件
        if (deviceId != null && !deviceId.isEmpty()) {
            queryWrapper.eq(EnergyReportDataDO::getDeviceId, deviceId);
        }
        
        // 时间范围条件
        if (startTime != null && endTime != null) {
            queryWrapper.between(EnergyReportDataDO::getReportDataTime, startTime, endTime);
        } else if (startTime != null) {
            queryWrapper.ge(EnergyReportDataDO::getReportDataTime, startTime);
        } else if (endTime != null) {
            queryWrapper.le(EnergyReportDataDO::getReportDataTime, endTime);
        }
        
        queryWrapper.orderByDesc(EnergyReportDataDO::getReportDataTime);
        
        return selectList(queryWrapper);
    }
} 